﻿CREATE PROCEDURE [dbo].[spCreateSequenceOrder]
	@OrderID int, 
	@SequenceID int
AS
begin
	declare @SequenceOrderID int

	insert into tblSequenceOrder ( OrderID, SequenceID, OwnerID, SequenceStatusCode, CanceledReasonID, AccountID, ShipTo_PersonID, ShipTo_CompanyName, ShipTo_AddressID, CreateDate )
	select @OrderID, @SequenceID, T1.OwnerID, 'A', null, T2.AccountID, T2.ShipTo_PersonID, T2.ShipTo_CompanyName, T2.ShipTo_AddressID, GETDATE()
	from tblSequence T1,
		tblOrderHeader T2
	where T1.SequenceID = @SequenceID
		and T2.OrderID = @OrderID
	
	set @SequenceOrderID = SCOPE_IDENTITY()

	exec spCreateSequenceStepsForOrder @SequenceID, @SequenceOrderID, null

	RETURN @SequenceOrderID
end